﻿using System;
using Super_Management_Mayhem.Content.Animations.Manager;
using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Content.Spritesheet.Player;
using Super_Management_Mayhem.Screens.Playing.Players;
using Transformable_Engine_v2.Engine.Animation.FrameAnimation;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Helper;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Menu.SelectAvatar
{
    public class Avatar : Container
    {
        Character _character;

        private Timer _rainbowTimer;

        private const int RAINBOW_TIME = 2000;

        public Avatar()
        {
            AddChildren(new ImageContainer {
                                               Sprite = ManagerSheet.GetSprite(ManagerSheet.Indicator.ShadowIndicator),
                                               Origin = TE_Rectangle.Alignment.CenterMiddle,
                                               Scale = new TE_Vector2(2, 2)
                                           });


            _character = new Character(Color.Red) {
                                                  Sprite = ManagerSheet.GetSprite(ManagerSheet.IdleState.Down),
                                                  Origin = TE_Rectangle.Alignment.BottomMiddle,
                                                  Scale = new TE_Vector2(2, 2)
                                              };

            AddChildren(_character);

            AddChildren(new NewIcon {
                                        PositionGridBound = new TE_Vector2(0, -4)
                                    });

            _rainbowTimer = new Timer(RAINBOW_TIME, true, null);
            _rainbowTimer.Start();
            AddTimers(_rainbowTimer);
        }

        public Avatar(Color color)
        {
            _character = new Character(color) {
                                                  Sprite = ManagerSheet.GetSprite(ManagerSheet.IdleState.Down),
                                                  Origin = TE_Rectangle.Alignment.BottomMiddle,
                                                  Scale = new TE_Vector2(2, 2),
                                              };

            AddChildren(_character);
        }

        public void WalkAnimationLeft()
        {
            _character.ClearAnimation<FrameAnimation>();
            _character.AddAnimations(new FrameAnimation(new WalkLeft(new ManagerSheet().SpriteSheetPath)));
        }

        public void WalkAnimationRight()
        {
            _character.ClearAnimation<FrameAnimation>();
            _character.AddAnimations(new FrameAnimation(new WalkRight(new ManagerSheet().SpriteSheetPath)));
        }

        public void Stop()
        {
            _character.ClearAnimation<FrameAnimation>();
            _character.Sprite = ManagerSheet.GetSprite(ManagerSheet.IdleState.Down);
        }

        public override void Update(System.TimeSpan gameTime)
        {
            if(_rainbowTimer != null)
            {
                float r = (float) (Math.Sin((_rainbowTimer.PercentageElapsed + 0.666).TimesTwoPi()) * 127 + 128);
                float g = (float) (Math.Sin((_rainbowTimer.PercentageElapsed + 0.333).TimesTwoPi()) * 127 + 128);
                float b = (float) (Math.Sin((_rainbowTimer.PercentageElapsed).TimesTwoPi()) * 127 + 128);

                _character.SetColor(new Color(r, g, b));
            }
            base.Update(gameTime);
        }
    }
}